package com.luohai.langchain4jspringboot.controller;

import com.luohai.langchain4jspringboot.aiservice.AiMessagesService;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.concurrent.*;

/**
 * @author luoh6
 */
@RestController
@RequestMapping(produces = "text/html;charset=utf-8")
public class AiController {

    @Autowired
    private AiMessagesService aiMessagesService;

    @Autowired
    @Qualifier("taskExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @GetMapping(value = "/chat")
    public Flux<String> getAiResponse(String memoryId,String message) throws ExecutionException, InterruptedException {
//        // 创建 Callable 任务
//        Callable<Flux<String>> task = () -> {
//            Flux<String> aiMessages = aiMessagesService.getAiMessages(memoryId, message);
//            return aiMessages;
//        };
//
//        Future<Flux<String>> result = threadPoolTaskExecutor.submit(task);
        return aiMessagesService.getAiMessages(memoryId, message);
    }
}
